/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.html.view;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.faces.event.ActionEvent;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import offset.nodes.server.view.ContainerBean;
import offset.nodes.server.dispatch.controller.DispatchAttribute;
import offset.nodes.server.controller.EditorAttribute;
import offset.nodes.server.html.controller.HtmlAttribute;
import offset.nodes.server.html.model.PageModel;
import offset.nodes.server.model.RepositoryUtils;
import offset.nodes.server.view.FacesUtils;
import offset.nodes.server.view.component.applet.AppletModel;

/**
 *
 * @author Walter Lütgenau
 */
public class HtmlBean extends ContainerBean {

    String path;
    Node node;
    InputStream content;

    protected void initComponents() {
        AppletModel exportBookModel = new AppletModel();
        exportBookModel.setRendered(false);
        getComponents().put("exportBook", exportBookModel);
    }

    public void renderExportBook(ActionEvent e) throws RepositoryException, IOException {
        AppletModel appletModel = (AppletModel) getComponents().get("exportBook");
        appletModel.setRendered(true);
    }

    protected void init() throws RepositoryException {
        HtmlAttribute att = (HtmlAttribute) FacesUtils.getRequest().getAttribute(HtmlAttribute.ATT_HTML);
        if (att == null) {
            path = FacesUtils.getPath();

            // reinit from browser page
             if (node == null) {
                 node = RepositoryUtils.getRepositoryModel().getNode(path);
                 content = new PageModel(RepositoryUtils.getRepositorySession()).getDownloadInfo(node).getInput();
                 return;
             }

            return;
        }

        // whenever current node changes, change all depending variables
        path = att.getPath();
        node = att.getNode();
        content = att.getDownloadAttribute().getInput();

        initComponents();
    }

    public String edit() {
        return "edit";
    }

    public String print() {
        return "print";
    }

    public String version() throws RepositoryException {
        DispatchAttribute dispatchInfo = new DispatchAttribute();
        dispatchInfo.setPath(FacesUtils.getPath());
        dispatchInfo.setNode(RepositoryUtils.getRepositoryModel().getNode(dispatchInfo.getPath()));
        FacesUtils.getRequest().setAttribute(DispatchAttribute.ATT_DISPATCH, dispatchInfo);
        
        return "versions";
    }

    public String references() throws RepositoryException {
        DispatchAttribute dispatchInfo = new DispatchAttribute();
        dispatchInfo.setPath(FacesUtils.getPath());
        dispatchInfo.setNode(RepositoryUtils.getRepositoryModel().getNode(dispatchInfo.getPath()));
        FacesUtils.getRequest().setAttribute(DispatchAttribute.ATT_DISPATCH, dispatchInfo);

        return "references";
    }

      public String getName() throws RepositoryException {
        init();
        
        int nameStart = path.lastIndexOf("/");

        if (nameStart < 0) {
            return path;
        }

        return path.substring(nameStart + 1);
    }

    public InputStream getContent() throws RepositoryException {
        init();

        if ( content == null)
            return new ByteArrayInputStream("".getBytes());
        
        return content;
    }

    public String getReturnUrl() {
        EditorAttribute editorAttribute = (EditorAttribute) FacesUtils.getRequest().getAttribute(EditorAttribute.ATT_EDITOR);
        if (editorAttribute == null || editorAttribute.getReturnUrl() == null)
            return "";

        return editorAttribute.getReturnUrl();
    }
    
}
